package com.xyb.service;

import com.xyb.dao.CourseDao;
import com.xyb.dao.CourseTopicDao;
import com.xyb.entity.Course;
import com.xyb.entity.CourseTopic;
import com.xyb.utils.RandomUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
public class WebCourseService {
    @Resource
    private CourseDao courseDao;
    @Resource
    private CourseTopicDao courseTopicDao;

    public List<Course> selectBaseCourseList() {
        List<Course> list = courseDao.selectCourseByTopicId(0L);
        return list;
    }

    public List<CourseTopic> selectTopicCourseAndCourseList() {
        List<CourseTopic> list = courseTopicDao.selectCourseTopicAndCourseList();
        for (int i = 0; i < list.size(); i++) {
            if(list.get(i).getTopicId().longValue()==0L){
                list.remove(i);
                break;
            }
        }
        return list;
    }

    public Course selectCourseByCourseId(Long courseId) {
        Course course = courseDao.selectCourseByCourseId(courseId);
        return course;
    }

    public List<Course> selectRecentCourseListByUserId(Long userId) {
        // todo 根据用户id 查询最近观看的课程
        List<Course> list = courseDao.selectCourseList();
        List<Course> list1 = new ArrayList<>();
        if(list!=null){
            for (int i = 0; i < 4; i++) {
                int randomInt = RandomUtils.getRandomInt(0, list.size()-1);
                list1.add(list.get(randomInt));
            }
            return list1;
        }
        return list;
    }

    public List<Course> selectHotCourseList() {
        List<Course> list = courseDao.selectCourseList();
        List<Course> list1 = new ArrayList<>();
        if(list!=null){
            for (int i = 0; i < 5; i++) {
                int randomInt = RandomUtils.getRandomInt(0, list.size()-1);
                list1.add(list.get(randomInt));
            }
            return list1;
        }
        return list;
    }
}
